"""
离场/停留识别模块API路由
"""
from fastapi import APIRouter, Query
from app.core.response import create_response
from app.services.mock_data import mock_generator
from app.models.schemas import PredictionRequest, BatchPredictionRequest

router = APIRouter()


@router.get("", summary="获取完整数据")
async def get_departure_retention_data():
    """
    获取离场/停留识别模块的所有数据
    """
    data = mock_generator.generate_departure_retention_data()
    return create_response(data=data)


@router.get("/classification", summary="获取实时分类统计")
async def get_flow_classification():
    """
    获取离场/停留人流实时分类统计
    """
    data = mock_generator.generate_flow_classification()
    return create_response(data=data)


@router.get("/trend", summary="获取趋势数据")
async def get_trend_data(
    period: str = Query(
        default="24h", regex="^(24h|7d|30d)$", description="时间段: 24h, 7d, 30d"
    )
):
    """
    获取离场/停留人流趋势数据

    Args:
        period: 时间段,可选值: 24h(24小时), 7d(7天), 30d(30天)
    """
    data = mock_generator.generate_trend_data(period)
    return create_response(data=data)


@router.get("/areas", summary="获取区域分布")
async def get_area_distribution():
    """
    获取各区域的离场/停留人流分布
    """
    full_data = mock_generator.generate_departure_retention_data()
    data = full_data["heatmapData"]
    return create_response(data=data)


@router.get("/confidence", summary="获取置信度分布")
async def get_confidence_distribution():
    """
    获取预测置信度分布
    """
    full_data = mock_generator.generate_departure_retention_data()
    data = full_data["confidenceDistribution"]
    return create_response(data=data)


@router.post("/predict", summary="单个预测")
async def predict_single(request: PredictionRequest):
    """
    对单个人员进行离场/停留预测

    Args:
        request: 预测请求数据
    """
    data = mock_generator.generate_prediction_result()
    return create_response(data=data, message="预测成功")


@router.post("/predict/batch", summary="批量预测")
async def predict_batch(request: BatchPredictionRequest):
    """
    批量预测人员的离场/停留状态

    Args:
        request: 批量预测请求数据
    """
    results = [
        mock_generator.generate_prediction_result() for _ in range(len(request.data))
    ]

    # 统计结果
    departure_count = sum(1 for r in results if r["prediction"] == "departure")
    retention_count = len(results) - departure_count

    data = {
        "results": results,
        "summary": {
            "total": len(results),
            "departure": departure_count,
            "retention": retention_count,
        },
    }

    return create_response(data=data, message="批量预测成功")
